package academic.ui.client.view;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.DomEvent;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;

import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.ViewImpl;

import ecommerce.soco.shared.GWT_Product;

import ui.client.GenericClickable;
import ui.client.GenericContainerTag;

import academic.ui.client.config.CSSConstants;
import academic.ui.client.presenter.CreateEventPresenter;
import academic.ui.client.widgets.forms.CreateEventForm;

public class CreateEventView extends ViewImpl implements CreateEventPresenter.MyView {

	public GenericContainerTag div = new GenericContainerTag("div");
	
	
	private CreateEventForm createEventForm = new CreateEventForm();
	private GenericContainerTag eventSpan = new GenericContainerTag("span","submit");
	private GenericClickable eventSubmitButton = new GenericClickable("a");
	
	public GenericContainerTag slot = new GenericContainerTag("div");
	public GenericClickable cancel = new GenericClickable("a","cancel");
	private GenericContainerTag createEventDiv = new GenericContainerTag("div");
	
	private KeyDownHandler submitFormHandler = new KeyDownHandler() {

		@Override
		public void onKeyDown(KeyDownEvent event) {
			if(event.getNativeKeyCode()==KeyCodes.KEY_ENTER) {
				NativeEvent e = Document.get().createClickEvent(0, 0, 0, 0, 0, false, false, false, false);
				DomEvent.fireNativeEvent(e, eventSubmitButton);
			}
		}
		
	};
	
	private GenericContainerTag loginBody = new GenericContainerTag("div");
	
	@Inject
	public CreateEventView(CSSConstants css) {
		super();
		div.setStyleName("createEvent");
		eventSpan.setStyleName("submitButton");
		eventSubmitButton.setStyleName("submitButtonLink");
		eventSubmitButton.add(eventSpan);
		loginBody.setStyleName("loginBody");

		loginBody.add(createEventDiv);
		createEventDiv.clear();		
		//add both forms
		createEventForm.reset();
		
		createEventDiv.add(createEventForm);
		createEventDiv.add(eventSubmitButton);
		createEventDiv.add(cancel);
		
		div.add(loginBody);
	}
	
	@Override
	public Widget asWidget() {
		return div;
	}

	/**
	 * Returns false if there are any errors
	 */
	@Override
	public Boolean checkRules() {
		return createEventForm.checkRules();
	}
	
	@Override
	public CreateEventForm getForm() {
		return createEventForm;
	}

	@Override
	public HasClickHandlers cancel() {
		return cancel;
	}
	
	@Override
	public HasClickHandlers submit() {
		return eventSubmitButton;
	}

	@Override
	public void setup(GWT_Product result) {
		createEventForm.initForm(result);
	}


}
